;(function ($,window,document,undefined){
    var defaults = {
        title:'请选择设备',
        root:null,//根路径
        keyId:null,
        keyType:null,
        confirmUrl: null,
        deviceId: null,
        typeStatus: null,//1 维修，2保养， 3安装
        callback:function(){}   //回调
    };
    var scoreArr = [];
    var scoreType = {};
    this.Score = function (element,options){
        var opts = options;
        var isSuccess = false;
        $obj = $(element);
        this.filling = function(opts,obj){
            $obj.iziModal({
                title: '确认',
                headerColor: '#00c0ef',
                width: 600,
                zindex: 1050,
                focusInput: false,
                group: '',
                loop: false,
                history: true,
                autoOpen: true,
                bodyOverflow: false,
                overlayClose: false,
                onFullscreen: function(){},
                onResize: function(){},
                onOpening: function(){},
                onOpened: function(){
                },
                onClosing: function(){
                },
                onClosed: function(){
                    $("#scoreTable").html("");
                    scoreArr = [];
                    $("textarea").val("");
                    $("input[name='one']").on('ifChecked', function(event){
                        var current = $(this).parents("tr").index();
                        $("[name='one']:checked").each(function(i){
                            if($(this).parents("tr").index()!=current){
                                $(this).iCheck('uncheck');
                            }
                        });
                    });
                }
            });

            if(opts.typeStatus == 1){
                scoreType = {typeName: "确认修复", yesText: "已修复", yesValue: "2204", noText: "未修复", noValue: "2205"};
            }else if(opts.typeStatus == 2){
                scoreType = {typeName: "确认保养", yesText: "已保养", yesValue: "1804", noText: "未保养", noValue: "1805"};
            }else if(opts.typeStatus == 3){
                scoreType = {typeName: "确认安装", yesText: "已安装", yesValue: "1203", noText: "未安装", noValue: "1204"};
            }

            var scoreTrs = "";
            for(var x=0;x<scoreArr.length;x++){
                scoreTrs += '<tr>\
                    <td>'+scoreArr[x].text+'</td>\
                    <td>\
                        <label>\
                            <input type="radio" class="flat-green" checked value="5" name="'+scoreArr[x].colName+'">\
                        </label>\
                    </td>\
                    <td>\
                        <label>\
                            <input type="radio" class="flat-green" value="4" name="'+scoreArr[x].colName+'">\
                        </label>\
                    </td>\
                    <td>\
                        <label>\
                            <input type="radio" class="flat-green" value="3" name="'+scoreArr[x].colName+'">\
                        </label>\
                    </td>\
                    <td>\
                        <label>\
                            <input type="radio" class="flat-green" value="2" name="'+scoreArr[x].colName+'">\
                        </label>\
                    </td>\
                    <td>\
                        <label>\
                            <input type="radio" class="flat-green" value="1" name="'+scoreArr[x].colName+'">\
                        </label>\
                    </td>\
                </tr>'
            }
            var html = '<div class="box-body">\
                            <form id="confirm-form" class="col-md-12 form-horizontal">\
                               <div class="form-group col-md-12">\
                                    <label class="col-md-3 control-label text-muted">'+scoreType.typeName+'</label>\
                                    <div class="col-md-9">\
                                        <div class="radio">\
                                            <label>\
                                                <input type="radio" class="flat-green" checked value="'+scoreType.yesValue+'" name="isSure">'+scoreType.yesText+'\
                                            </label>\
                                            <label>\
                                                <input type="radio" class="flat-green" value="'+scoreType.noValue+'" name="isSure">'+scoreType.noText+'\
                                            </label>\
                                        </div>\
                                    </div>\
                               </div>\
                               <div class="form-group col-md-12">\
                                    <label  class="col-md-3 control-label text-muted">说明</label>\
                                    <div class="col-md-9">\
                                        <textarea class="form-control" rows="2" id="remark"></textarea>\
                                    </div>\
                               </div>\
                               <div class="form-group col-md-12">\
                                    <table class="table">\
                                        <thead>\
                                            <tr>\
                                                <th></th>\
                                                <th>满意</th>\
                                                <th>比较满意</th>\
                                                <th>一般</th>\
                                                <th>不太满意</th>\
                                                <th>不满意</th>\
                                            </tr>\
                                        </thead>\
                                        <tbody id="scoreTable">'+scoreTrs+'</tbody>\
                                    </table>\
                               </div>\
                               <div class="form-group col-md-12">\
                                    <label  class="col-md-3 control-label text-muted">评论</label>\
                                    <div class="col-md-9">\
                                        <textarea class="form-control" rows="2" id="comment"></textarea>\
                                    </div>\
                               </div>\
                            </form>\
                       </div>\
                       <div class="box-footer">\
                            <span class="pull-right">\
                                <button data-iziModal-close class="btn btn-default" style="margin-right: 5px">关闭</button>\
                                <button  class="btn btn-info" name="saveConfirm" style="margin-right: 5px">提交</button>\
                            </span>\
                        </div>\
                       '
            $obj.find(".iziModal-content").html(html);
            $('input.flat-green').iCheck({
                checkboxClass: 'icheckbox_flat-green',
                radioClass: 'iradio_flat-green'
            });
            this.eventBind = function(){
                var self = this;
                //必须先解绑再绑定事件，否则多次执行后会多次触发
                $obj.off('click','button[name="saveConfirm"]').on('click','button[name="saveConfirm"]',function(){
                    var commentStatus = $("input[name='isSure']:checked").val();
                    var commentScoreList = [];
                    for(var x=0;x<scoreArr.length;x++){
                        commentScoreList.push({dividType: scoreArr[x].colName, value: $("input[name='"+scoreArr[x].colName+"']:checked").val()})
                    }
                    //确认接口
                    var confirData = {};
                    if(opts.typeStatus == 1){
                        confirData.repairId = opts.keyId;
                    }else if(opts.typeStatus == 2){
                        confirData.upkeepId = opts.keyId;
                    }else{
                        confirData.installId = opts.keyId;
                        confirData.deviceId = opts.deviceId;
                    }
                    confirData.status = $("input[name='isSure']:checked").val();
                    confirData.comment = $("#remark").val();

                    var submitData = {
                        keyId: opts.keyId,
                        keyType: opts.keyType,
                        commentContent: $("#comment").val(),
                        jsonStr: JSON.stringify(commentScoreList)
                    }

                    //提交确认信息
                    publicAjax(opts.root+opts.confirmUrl,"POST",confirData,function (data) {
                        if (data.ret == 200) {
                            //提交评论信息
                            publicAjax(opts.root+"comment/addComment","POST",submitData,function (datas){
                                if(datas.ret == 200){
                                    isSuccess = true;
                                    $obj.iziModal("close");
                                    typeof opts.callback === 'function' && opts.callback(self);
                                }else{
                                    isSuccess = false;
                                }
                            })
                        } else {
                            isSuccess = false;
                        }
                    })                  
                });
            }
        }
        this.returnIsSuccess = function(){
            return isSuccess;
        };
        this.getScoreType = function(){
            $.ajax({
                type: "POST",
                url: opts.root+"base/queryItem",
                data: {type: "divid_type"},
                async: false,
                success:function(data){
                    if(data.ret == 200){
                        for(var x=0;x<data.data.length;x++){
                            scoreArr.push({text: data.data[x].item, colName: data.data[x].itemId});
                        }
                    }
                }
            })
        }
        this.init = function (){
            if(opts.typeStatus == 1){
                scoreType = {typeName: "确认修复", yesText: "已修复", yesValue: "2204", noText: "未修复", noValue: "2205"}
            }else if(opts.typeStatus == 2){
                scoreType = {typeName: "确认保养", yesText: "已保养", yesValue: "1804", noText: "未保养", noValue: "1805"}
            }else{
                scoreType = {typeName: "确认安装", yesText: "已验收", yesValue: "1203", noText: "未验收", noValue: "1204"}
            }
            this.getScoreType();
            this.filling(opts,this);
            this.eventBind();
        }
        this.init();
    }
    $.fn.confirmData = function(parameter,callback){
        if(typeof parameter == 'function'){//重载
            callback = parameter;
            parameter = {};
        }else{
            parameter = parameter || {};
            callback = callback || function(){};
        }
        var options = $.extend({},defaults,parameter);
        callback(new Score(this, options));
    };
})(jQuery, window,document);